#include "headfile.h"
HGE *hge=0;

HTEXTURE			tex;
hgeQuad				quad;


hgeFont				*fnt;
hgeSprite			*spr;

GUIPageManager* PageMgr;

bool FrameFunc()
{
	float dt=hge->Timer_GetDelta();
	static int lastid=0;

	if(hge->Input_GetKeyState(HGEK_ESCAPE)) { lastid=5; PageMgr->PageLogin->Leave(); }

	PageMgr->Update(dt);

	quad.v[0].tx=0;        quad.v[0].ty=0;
	quad.v[1].tx=1;        quad.v[1].ty=0;
	quad.v[2].tx=1;        quad.v[2].ty=1;
	quad.v[3].tx=0;        quad.v[3].ty=1;
	return false;
}


bool RenderFunc()
{
	hge->Gfx_BeginScene();
	hge->Gfx_RenderQuad(&quad);
	PageMgr->Render();
	fnt->SetColor(0xFFFFFFFF);
	fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d", hge->Timer_GetDelta(), hge->Timer_GetFPS());
	hge->Gfx_EndScene();

	return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_LOGFILE, "../Log/HGEDemo.log");
	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
	hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
	hge->System_SetState(HGE_TITLE, "HGEDemo");
	hge->System_SetState(HGE_WINDOWED, true);
	hge->System_SetState(HGE_SCREENWIDTH, 800);
	hge->System_SetState(HGE_SCREENHEIGHT, 600);
	hge->System_SetState(HGE_SCREENBPP, 32);
	hge->System_SetState(HGE_SHOWSPLASH, false);
	if(hge->System_Initiate())
	{
		PageMgr=new GUIPageManager();
		quad.tex=hge->Texture_Load("../Resource/bg.jpg");
		tex=hge->Texture_Load("../Resource/cursor.png");
		if(!quad.tex || !tex )
		{
			MessageBox(NULL, "Can't load BG.PNG, CURSOR.PNG", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
			hge->System_Shutdown();
			hge->Release();
			return 0;
		}
		quad.blend=BLEND_DEFAULT;

		for(int i=0;i<4;i++)
		{
			quad.v[i].z=0.5f;
			quad.v[i].col=0xFFFFFFFF;
		}

		quad.v[0].x=0.0f; quad.v[0].y=0.0f; 
		quad.v[1].x=hge->System_GetState(HGE_SCREENWIDTH)+300; quad.v[1].y=0.0f; 
		quad.v[2].x=hge->System_GetState(HGE_SCREENWIDTH)+300; quad.v[2].y=hge->System_GetState(HGE_SCREENHEIGHT)+500; 
		quad.v[3].x=0.0f; quad.v[3].y=hge->System_GetState(HGE_SCREENHEIGHT)+500; 

		fnt=new hgeFont("../Resource/font1.fnt");
		spr=new hgeSprite(tex,0,0,32,32);

		PageMgr->PageLogin->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
		PageMgr->PageLogin->SetCursor(spr);
		PageMgr->PageLogin->SetFocus(1);
		PageMgr->PageLogin->Enter();
		PageMgr->OpenPage(PageMgr->PageLogin);
		hge->System_Start();

		delete fnt;
		delete spr;
		hge->Texture_Free(tex);
		hge->Texture_Free(quad.tex);
	}
	hge->System_Shutdown();
	hge->Release();
	return 0;
}
